<?php
Yii::import('application.components.Reference');
Yii::import('application.components.U');
class TransferItemController extends GxController
{
    public function actionCreateIn()
    {
        if (!Yii::app()->request->isAjaxRequest)
            $this->redirect(url('/'));
        if (isset($_POST) && !empty($_POST)) {
            $status = false;
            $msg = "Stored data failed.";
            $detils = CJSON::decode($_POST['detil']);
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $model = new TransferItem;
                $ref = new Reference();
                $docref = $ref->get_next_reference(SUPPIN);
                foreach ($_POST as $k => $v) {
                    if ($k == 'detil') continue;
                    if (is_angka($v)) $v = get_number($v);
                    $_POST['TransferItem'][$k] = $v;
                }
                $_POST['TransferItem']['doc_ref'] = $docref;
                $model->attributes = $_POST['TransferItem'];
                if (!$model->save())
                    throw new Exception(t('save.model.fail','app',array('{model}'=>'Receive item')) . CHtml::errorSummary($model));
                foreach ($detils as $detil) {
                    $item_details = new TransferItemDetails;
                    $_POST['TransferItemDetails']['barang_id'] = $detil['barang_id'];
                    $_POST['TransferItemDetails']['qty'] = get_number($detil['qty']);
                    $_POST['TransferItemDetails']['transfer_item_id'] = $model->transfer_item_id;
                    $item_details->attributes = $_POST['TransferItemDetails'];
                    if (!$item_details->save())
                        throw new Exception(t('save.model.fail','app',array('{model}'=>'Receive item detail')) . CHtml::errorSummary($item_details));
                    U::add_stock_moves(SUPPIN, $model->transfer_item_id, $model->tgl,
                        $item_details->barang_id, $item_details->qty, $model->doc_ref,0);
                }
                $ref->save(SUPPIN, $model->transfer_item_id, $docref);
                $transaction->commit();
                $msg = t('save.success','app');
                $status = true;
            }catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'id' => $docref,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionCreateOut()
    {
        if (!Yii::app()->request->isAjaxRequest)
            $this->redirect(url('/'));
        if (isset($_POST) && !empty($_POST)) {
            $status = false;
            $msg = "Stored data failed.";
            $detils = CJSON::decode($_POST['detil']);
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $model = new TransferItem;
                $ref = new Reference();
                $docref = $ref->get_next_reference(SUPPOUT);
                foreach ($_POST as $k => $v) {
                    if ($k == 'detil') continue;
                    if (is_angka($v)) $v = get_number($v);
                    $_POST['TransferItem'][$k] = $v;
                }
                $_POST['TransferItem']['doc_ref'] = $docref;
                $model->attributes = $_POST['TransferItem'];
                if (!$model->save())
                    throw new Exception(t('save.model.fail','app',array('{model}'=>'Return item')) . CHtml::errorSummary($model));
                foreach ($detils as $detil) {
                    $item_details = new TransferItemDetails;
                    $_POST['TransferItemDetails']['barang_id'] = $detil['barang_id'];
                    $_POST['TransferItemDetails']['qty'] = -get_number($detil['qty']);
                    $_POST['TransferItemDetails']['transfer_item_id'] = $model->transfer_item_id;
                    $item_details->attributes = $_POST['TransferItemDetails'];
                    if (!$item_details->save())
                        throw new Exception(t('save.model.fail','app',array('{model}'=>'Return item detail')) . CHtml::errorSummary($item_details));
                    U::add_stock_moves(SUPPOUT, $model->transfer_item_id, $model->tgl,
                        $item_details->barang_id, $item_details->qty, $model->doc_ref,0);
                }
                $ref->save(SUPPOUT, $model->transfer_item_id, $docref);
                $transaction->commit();
                $msg = t('save.success','app');
                $status = true;
            }catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'id' => $docref,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionIndexIn()
    {
        $criteria = new CDbCriteria();
        $criteria->addCondition("type_ = 0 AND DATE(tgl) = :tgl");
        $criteria->params = array(':tgl'=>$_POST['tgl']);
        $model = TransferItem::model()->findAll($criteria);
        $total = TransferItem::model()->count($criteria);
        $this->renderJson($model, $total);
    }
    public function actionIndexOut()
    {
        $criteria = new CDbCriteria();
        $criteria->addCondition("type_ = 1 AND DATE(tgl) = :tgl");
        $criteria->params = array(':tgl'=>$_POST['tgl']);
        $model = TransferItem::model()->findAll($criteria);
        $total = TransferItem::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}